package excel

import (
	"github.com/xuri/excelize/v2"

	s "gitee.com/xiaoa7/jsetl/service"
)

type (
	SheetColMapping struct {
		fieldMapping              map[string]string //Sheet字段映射
		fieldColName2IndexMapping map[string]int    //
		fieldIndex2ColNameMapping map[int]string
	}
	Excel struct {
		file              *excelize.File
		SheetColMappinges map[string]*SheetColMapping
	}
)

// 打开excel文件
func OpenExcelFile(key, file string) error {
	if f, err := excelize.OpenFile(file); err == nil {
		s.RegisteService(key, &Excel{f, make(map[string]*SheetColMapping)})
		return nil
	} else {
		return err
	}
}

// 空实现
func (r *Excel) Connect(key string, p ...string) error {
	return nil
}

// 断开连接
func (r *Excel) Destory() error {
	if r.file != nil { //保存
		r.file.Save()
	}
	return nil
}
